package namespace

import (
	"context"
	"kms-backend/config"
	"kms-backend/controllers"
	"kms-backend/utils/logs"

	"github.com/gin-gonic/gin"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Delete(r *gin.Context) {
	logs.Debug(nil, "开始删除命名空间")

	returnData := config.ReturnData{}
	// 生成clientSet, 基础数据basicInfo
	clientSet, basicInfo, err := controllers.BasicInit(r, nil)
	if err != nil {
		returnData.Message = err.Error()
		returnData.Status = 400
		r.JSON(200, returnData)
		return
	}

	// 禁止删除的namespace

	if basicInfo.Name == "kube-system" {
		returnData.Message = "禁止删除kube-system"
		returnData.Status = 400
		r.JSON(200, returnData)
		return
	}

	// 删除操作
	err = clientSet.CoreV1().Namespaces().Delete(context.TODO(), basicInfo.Name, metav1.DeleteOptions{})
	if err != nil {
		msg := "删除命名空间失败" + err.Error()
		logs.Error(nil, "集群"+basicInfo.ClusterId+msg)
		returnData.Message = msg
		returnData.Status = 400
	} else {
		msg := "删除命名空间成功"
		logs.Info(nil, "集群"+basicInfo.ClusterId+"中命名空间"+basicInfo.Name+"删除成功")
		returnData.Message = msg
		returnData.Status = 200
	}
	r.JSON(200, returnData)

}
